package com.pet.item.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.common.domain.PageDTO;
import com.pet.common.domain.Result;
import com.pet.item.domain.query.StoreQuery;
import com.pet.item.domain.po.Store;
import com.pet.item.domain.vo.StoreBriefVO;
import com.pet.item.domain.vo.StoreDetailVO;
import com.pet.item.domain.vo.StoreMapVO;
import com.pet.item.mapper.StoreMapper;
import com.pet.item.service.IStoreService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 商店信息表 服务实现类
 * </p>
 *
 * @author Cookies
 * @since 2024-07-03
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
    @Override
    public PageDTO<StoreDetailVO> getStoreList(StoreQuery storeQuery) {
        Page<Store> page = new Page<>(storeQuery.getPageNo(), storeQuery.getPageSize());
        lambdaQuery()
                .like(StrUtil.isNotBlank(storeQuery.getKeyWord()), Store::getName, storeQuery.getKeyWord())
                .page(page);
        PageDTO<StoreDetailVO> pageDTO = PageDTO.of(page, StoreDetailVO.class);
        return pageDTO;
    }

    @Override
    public List<StoreMapVO> getStoreListForMap() {
        List<Store> storeList = lambdaQuery().list();
        List<StoreMapVO> storeDetailVOList = BeanUtil.copyToList(storeList, StoreMapVO.class);
        for (StoreMapVO storeDetailVO : storeDetailVOList) {
            storeDetailVO.setRating(RandomUtil.randomInt(1, 5));
            storeDetailVO.setCommentCount(RandomUtil.randomInt(1, 100));
        }
        return storeDetailVOList;
    }

    @Override
    public boolean addStore(Store store) {
        // 随机纬度范围24.622500~24.625800
        store.setLatitude(BigDecimal.valueOf(RandomUtil.randomDouble(24.622500, 24.625800)));
        // 随机经度范围118.086100~118.089600
        store.setLongitude(BigDecimal.valueOf(RandomUtil.randomDouble(118.086100, 118.089600)));
        return save(store);
    }

    @Override
    public Result getStoreBriefById(Integer id) {
        if (id == null) {
            return Result.error("商店id不能为空");
        }
        Store store = getById(id);
        if (store == null) {
            return Result.error("商店不存在");
        }
        StoreBriefVO storeBriefVO = BeanUtil.copyProperties(store, StoreBriefVO.class);
        storeBriefVO.setRating(RandomUtil.randomInt(1, 5));
        storeBriefVO.setCommentCount(RandomUtil.randomInt(1, 100));
        return Result.success(storeBriefVO);
    }
}
